require'./Generic_chromosome.rb'
require'./Generic_gen.rb'
require'./Super_gen.rb'
require'./Gen_traductor.rb'
require'./Fitness_function.rb'
require'./Translator_chromosome.rb'
require'./Tournament.rb'
require'./Cross.rb'
require'./Basic_mutation.rb'

class Main
	def newChromosome()
		Translator_chromosome.new([newSuperGen,newSuperGen,newSuperGen],Fitness_function.new)
	end

	def newSuperGen
		genTipoLibro = Generic_gen.new(["CienciaFiccion","Ensayo","Otro"])
		genIdioma = Generic_gen.new(["Frances","Ingles","Ruso"])
		genCantidadPaginas = Generic_gen.new([290,"Otro"])
		genLongitud = Generic_gen.new(["Breve","Intermedio","Mas extenso"])
		Gen_traductor.new([genTipoLibro,genIdioma,genCantidadPaginas,genLongitud])
	end

	def main
		population = []
		newpop = []
		breedpool = []
		mutated = []
		times_mutated = 0
		128.times{population << newChromosome}
		best = []
		500.times{
#			puts population.size
			population.each{|c| c.evaluate}
#			cb = population.sort{|a,b|a.fitness_value <=> b.fitness_value}.last
#			p "repeated? : #{best.include?(cb)}"
#			best << cb
#			p "best chromosome : #{cb}"
#			p "best fitness : #{cb.fitness_value}"
#			p "best soo far : #{best.size}"
			breedpool += Tournament.new(16,population).select
			newpop += Simple_cross.new(breedpool).breed
			mutated += Basic_mutation.new(newpop,0.01,0.001).mutate
#uncomment to discart original muttated chromosomes
			newpop -= mutated
			times_mutated += mutated.size
			newpop += mutated
			mutated.clear
			population.clear
			population += newpop
# comment line below to discart all chromosomes from previos generation
			population += breedpool
			newpop.clear
			breedpool.clear
		}
			population.each{|c| c.evaluate}
			c = population.sort{|a,b|a.fitness_value <=> b.fitness_value}.last
			t = c.get_silvia
			puts "silvia:"
	 		puts t.get_book
			puts t.get_languaje
			puts t.get_pages
			puts t.get_length
			puts "====="
			t = c.get_olga
			puts "olga:"
	 		puts t.get_book
			puts t.get_languaje
			puts t.get_pages
			puts t.get_length
			puts "====="
			t = c.get_juan
			puts "juan:"
	 		puts t.get_book
			puts t.get_languaje
			puts t.get_pages
			puts t.get_length
			puts "fitness:"
			puts c.fitness_value
			p "chromosomes mutatted : #{times_mutated}"
	end
end

Main.new.main
